const workboxBuild = require('workbox-build');

// NOTE: This should be run *AFTER* all your assets are built
// CONFIG: https://developers.google.com/web/tools/workbox/modules/workbox-build#generateswstring_mode
function buildSW() {
	// This will return a Promise
	return workboxBuild.generateSW({
		cacheId: 'game-demo', // 设置前缀
		skipWaiting: true, // 强制等待中的 Service Worker 被激活
		clientsClaim: true, // Service Worker 被激活后使其立即获得页面控制权
		globDirectory: 'bin',
		globPatterns: [
			'**\/*.{html,png,atlas,sk,json,js,css}',
		],
		globIgnores: ['sw.js', 'unpack.json'], // 忽略的文件
		swDest: 'bin/sw.js',
		runtimeCaching: [ // 配置路由请求缓存
			{
				urlPattern: /.*\.js/, // 匹配文件
				handler: 'networkFirst' // 网络优先
			}
		]
	});
};


buildSW().then(() => {
	console.log('build complete');
});